#!/usr/bin/env python
# coding: utf-8
# @Time    : 2021/11/11 16:40
# @Author  : YD
# @Email   : dinhe_1985@126.com 
# @File    : test.py
# @Software: PyCharm
# @Blog    : www.noteshare.cn
from conf.api_conf import *
from lib import zbx_api

zabbix = zbx_api.AutoZabbix(ZABBIX_SERVER, ZABBIX_USER, ZABBIX_PASSWORD)

# 新增Web监控
for server_name in WEB_STEP_URL:
    url_json = {
        "name": "{}-{}".format(WEB_NAME, server_name),
        "hostid": "{}".format(HOST_ID),
        "applicationid": "{}".format(APPLICATION_ID),
        "agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36",
        "steps": [
            {
                "name": "{}".format(server_name),
                "url": "{}".format(WEB_STEP_URL[server_name]),
                "required": "up",
                "no": 1
            }
        ]}
    print(url_json)
    zabbix.add_webmonitor(url_json)

# 新增Web监控触发器
for server_name in WEB_STEP_URL:
    web_fail_trigger_json = {
        "priority": "4",
        "description": "{}-{}-返回值".format(WEB_NAME, server_name),
        "expression": "{{CustomMonitor:web.test.fail[{}-{}].count(#3,0,\"ne\")}}>=2".format(WEB_NAME, server_name),
    }
    web_rspcode_trigger_json = {
        "priority": "4",
        "description": "{}-{}-响应代码".format(WEB_NAME, server_name),
        "expression": "{{CustomMonitor:web.test.rspcode[{}-{},{}].last()}}<>200".format(WEB_NAME, server_name,
                                                                                        server_name),
    }
    zabbix.add_trigger(web_fail_trigger_json)
    zabbix.add_trigger(web_rspcode_trigger_json)
